import Vue from "vue";

export const is_empty = (obj) => {
    return obj === undefined || obj === null || obj.length === 0
}

export const isDefined = (val) => {
    return val !== undefined && val !== null;
};

export const remove = function (arr, index) {
    if (index > -1) {
        arr.splice(index, 1);
    }
};

export const find_el = function (arr, col, val) {
    for (let i = 0; i < arr.length; i++) {
        if (val === arr[i][col]) {
            return {
                obj: arr[i], index: i
            }
        }
    }
    return null
}

export const removeChildren = (parent) => {
    for (let i = parent.childNodes.length; i > 0; i--) {
        parent.removeChild(parent.childNodes[i - 1])
    }
}

export const sum = (arr, from, end) => {
    let s = 0;
    from = from === undefined ? 0 : from
    end = end === undefined ? arr.length : end
    for (let i = from; i < end; i++) {
        s += arr[i];
    }
    return s;
}

export const computeHeight = (percent, list, columnNumber) => {
    for (let i = 0; i < list.length; i += columnNumber) {
        let range = Math.min(list.length - i, columnNumber)
        for (let j = 0; j < range; j++) {
            let th = list[i + j].height , tw = list[i + j].width
            percent.push(tw / th)
        }
    }
    reComputeHeight(percent, list, columnNumber)
}

export const reComputeHeight = (percent, list, columnNumber) => {
    for (let i = 0; i < list.length; i += columnNumber) {
        let range = Math.min(list.length - i, columnNumber),
            sumPer = sum(percent, i, i + range)
        for (let j = 0; j < range; j++) {
            let per = percent[i + j] * 100 / sumPer
            list[i + j]['perWidth'] =  per + '%'
        }
    }
}
